package com.itheima.controller;

import com.itheima.pojo.Goods;
import com.itheima.pojo.Result;
import com.itheima.service.GoodsService;
import com.mysql.jdbc.log.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhangyan
 * @date 2021/2/24 15:20
 */

@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @RequestMapping("/findByPage")
    // Map<String,Object> map 接收前端传过来的 this.pagination
    public Result findByPage(@RequestBody Map<String, Object> map) {
        log.debug("findByPage:" + map);
        Map<String, Object> resultMap = goodsService.findByPage(map);
        log.info("查询成功");
        return new Result(true, "分页查询成功!", resultMap);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Goods goods) {
        log.debug("add:" + goods);
        goodsService.add(goods);
        log.info("添加商品信息成功");
        return new Result(true, "添加商品信息成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.debug("findById:" + id);
        Goods goods = goodsService.findById(id);
        log.info("根据id查询成功");
        return new Result(true, "根据id查询成功", goods);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Goods goods){
        goodsService.edit(goods);
        return new Result(true,"修改商品信息成功");
    }

    @RequestMapping("/delById")
    public Result delById(Integer id) {
        log.debug("delById:" + id);
        goodsService.delById(id);
        log.info("删除成功");
        return new Result(true, "删除成功");
    }
}
